<?php

class fLanguageSourceIni implements fLanguageSourceInterface {
	private $_dir;
	protected $_sources = array();
	protected $_culture;
	
	public function __construct($culture) {
		$this->_culture = $culture;
		$this->_dir = _LANGUAGES_PATH_.fFactory::getApplication()->getName()._DS_.$culture._DS_;
	}
	
	public function setCulture($culture) {
		$this->_culture = $culture;				
	}
	
	/**
	 * Translate
	 *
	 * @param string $string
	 * @return string
	 */
	public function translate($string) {
		return $this->_sources[$string];
	}
	
	/**
	 * Import
	 *
	 * @param string $namespace
	 * @param boolean $require
	 * @throws fException
	 */
	public function import($namespace, $require = true) {
		$file = $this->_dir.$namespace.'.ini';
		if (!file_exists($file)) {
			if ($require)
				throw new fException('File language: "'. $file .'" does not exists!');
			else 
				return;
		}		
		if (fConfig::get('environment') == fApplication::ENV_DEV) {
			$lang = parse_ini_file($file, true);
		}
		else {
			//Cache				 
		}
		
		self::add($lang);				
	}
	
	/**
	 * Add source
	 *
	 * @param array $source
	 */
	public function add($source) {
		$source = (array) $source;
		$this->_sources = array_merge($this->_sources, $source);		
	}
	
	/**
	 * Clear
	 *
	 * @param mixed $singular	.$singular = null, clear all.
	 */
	public function clear($singular = null) {
		if ($singular == null) {
			$this->_sources = array();		
		}
		else {
			unset($this->_sources[$singular]);
		}
	}
}